package com.xiaoyu.lanling.feature.gift.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.emoji.widget.EmojiTextView
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.gift.GiftSelectEvent
import com.xiaoyu.lanling.feature.gift.model.Gift
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil

class GiftViewHolder : ViewHolderBase<Gift>() {

    companion object {
        private val sOnItemClickListener = View.OnClickListener { v: View? ->
            val item: Gift = v.getViewTag<Gift>() ?: return@OnClickListener
            if (!item.canGive) {
                return@OnClickListener
            }
            GiftSelectEvent(item).post()
        }
    }

    private var mSelectedDesView: EmojiTextView? = null
    private var mGiftLayout: LinearLayout? = null
    private var mGiftImageView: SimpleDraweeView? = null
    private var mGiftTitleView: TextView? = null
    private var mPriceDesView: TextView? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view: View = layoutInflater.inflate(R.layout.gift_item, parent, false)
        mSelectedDesView = view.findViewById(R.id.selected_des)
        mGiftLayout = view.findViewById(R.id.gift_layout)
        mGiftImageView = view.findViewById(R.id.gift_image)
        mGiftTitleView = view.findViewById(R.id.gift_title)
        mPriceDesView = view.findViewById(R.id.price_des)
        mGiftLayout?.setOnClickDebounceListener(sOnItemClickListener)
        return view
    }

    override fun showData(position: Int, itemData: Gift) {
        ImageViewUtil.loadImage(mGiftImageView, ImageLoadParam.newBuilder().setUrl(itemData.icon).setSizeInDP(60).build())
        if (itemData.canGive) {
            mGiftLayout?.alpha = 1f
            mGiftLayout?.isSelected = itemData.isSelected
            mGiftTitleView?.setTextColor(AppContext.getColor(if (itemData.isSelected) R.color.colorSecondary else R.color.colorThriceText))
        } else {
            mGiftLayout?.alpha = 0.5f
            mGiftLayout?.isSelected = false
            mGiftTitleView?.setTextColor(AppContext.getColor(R.color.cpColorTertiaryText))
        }
        mGiftTitleView?.text = itemData.name
        mPriceDesView?.text = AppContext.getString(R.string.gift_coin_desc, itemData.coin)
        mSelectedDesView?.setTextColor(itemData.getIntimacyTitleColor())
        mGiftTitleView?.setTextColor(itemData.getGiftTitleColor())
        mPriceDesView?.setTextColor(itemData.getPriceDesColor())
        mGiftLayout?.setViewTag(itemData)
    }
}